package zfbpay

import (
	"context"

	"github.com/go-pay/gopay"
	"github.com/go-pay/gopay/alipay"
)

type (
	// PayClient is alipay client.
	PayClient struct {
		appID      string
		privateKey string
		publicKey  string
		isProd     bool

		client *alipay.Client
	}

	Option func(*PayClient)
)

var (
	privateKey string
	publicKey  string
	appID      string
	isProd     bool
)

func defaultOption() *PayClient {
	return &PayClient{
		appID:      appID,
		privateKey: privateKey,
		publicKey:  publicKey,
		isProd:     isProd,
	}
}

// NewAliPay return new alipay client
func NewAliPay(opts ...Option) *PayClient {
	p := defaultOption()
	for _, opt := range opts {
		opt(p)
	}
	return p
}

// Init alipay client
func (p *PayClient) Init() (*PayClient, error) {
	client, err := alipay.NewClient(p.appID, p.privateKey, p.isProd)
	if err != nil {
		return nil, err
	}
	p.client = client
	return p, nil
}

// TradeAppPay app pay
func (p *PayClient) TradeAppPay(ctx context.Context, bm gopay.BodyMap) (string, error) {
	return p.client.TradeAppPay(ctx, bm)
}

// TradeWapPay Wap pay
func (p *PayClient) TradeWapPay(ctx context.Context, bm gopay.BodyMap) (string, error) {
	return p.client.TradeWapPay(ctx, bm)
}

// Client is alipay client.
func (p *PayClient) Client() *alipay.Client {
	return p.client
}
